/**
 * 
 */

/* START OF CV UPLOAD FILE*/

/* BELOW IS THE FUNCTION USED TO PROCESS THE UPLOAD CV FILES ,WRITTEN BY XIAOHUIMA */

/*FETCH THE CV FILES DATA INTO AN JS ARRAY*/
var management_team_string = $("#data-company_cv_files").html();
    management_team_string = management_team_string.replace(/\s+/g,"");
var management_team_arr = management_team_string.split(';');

window.onload = createUploader;

/*CREATE THE UPLOADER AND CREATE THE CV FILES LIST*/
function createUploader(){

	/*ALREADY EXISTS FILES*/
	//var cv_files_exists = $("#data-company_cv_files").html();
    //cv_files_exists = cv_files_exists.replace(/\s+/g,"");
    
    /* DATA ID*/
    var data_id = $("#data-id").attr("value");

    /*CV UPLOAD ACTION URL*/
    var cv_upload_action_url = $("#cv_upload_action_url").html();
    
	/*CREATE THE UPLOADER*/
    var uploader = new qq.FileUploader({
        element: document.getElementById('cv_upload_div'),
        action: cv_upload_action_url,
        debug: true,
        //params: { cv_files_exists: cv_files_exists , data_id: data_id },
        params: { data_id: data_id },
        onSubmit:function(){
            /* do something when uploading,this function may be define on other place ,by MaXiaoHui*/
            cv_upload_onsubmit();
        },
        onComplete: function(id, fileName, responseJSON){

            /*CHECK THE UPLOAD ACTION RESULT ,CREATE A DELETE LINK IF SUCCESS*/
            if( responseJSON.success == true){
            	/*ADD THE FILENAME TO THE DATA-MANAGEMENT_TEAM'S TEXTAREA AFTER UPLOAD THE FILE*/
                $("#data-company_cv_files").html(function(index,html){
                	return  html.replace(/\s+/g,"") + responseJSON.filename + ';' ;               	
                    });

                /*ADD A NEW LINK TO THE CV FILES LIST*/
                /*var delete_link = '<li id="' + responseJSON.filename + '"> <span>' + 
                           responseJSON.filename + 
                          '</span><a href="javascript:void(0)" onClick="cv_delete_action(\''+ responseJSON.filename +'\',this)">&nbsp;Delete</a></li>';

                $("#cv_delete_list").append(delete_link);*/
                
                
                $("#cv_upload_row_content").css('display', 'none');
                var cv_upload_file_owner = $(".cv_upload_file_ownername").val();
				var content = '<div class="cv_upload_item" id="'+responseJSON.filename.replace('.','_')+'">'
					         +'<div class="cv_upload_block"><input type="text" name="cv_file[]" value="'+responseJSON.filename+'"></div>'
				             +'<div class="cv_upload_block" id="cv_upload_block_button"><a href="" onClick="cv_delete_action(\''+ responseJSON.filename +'\',this); return false">&nbsp;Delete</a></div>'
							 +'<div class="cv_upload_block"><input type="text" name="cv_name[]" width="100px" value="'+ cv_upload_file_owner +'" ></div>'
							 +'</div>';
                $("#cv_upload_row_list").append(content);
                
                cv_upload_oncomplete();
                }           
            },
    });  

    /*CREATE CV LIST*/
}

/*PROCESS THE CV FILES DELETE ACTION,1.SENT A JSON REQUEST TO THE SERVER;2HIDE THE DELETE LINK*/
function cv_delete_action(filename,object){

	/*AJAX REQUEST TO REMOVE THE FILE*/    
	 var cv_delete_url = $('#cv_delete_action_url').html() + '?cv_delete_file=' + filename;
	 var data_id = $("#data-id").attr("value");
	 var id = '#'+filename.replace('.','_');
	 //var cv_files = $("#data-company_cv_files").html();
	     //cv_files = cv_files.replace(/\s+/g,"");
	     
	 //var post_data = 'data_id=' + data_id + '&cv_delete_file=' + filename + '&cv_files=' + cv_files ;
	 var post_data = 'data_id=' + data_id + '&cv_delete_file=' + filename ;
     $.ajax({
         url: cv_delete_url,
         type: "POST",
         data: post_data,
         dataType: "json",
         success: function(data,textStatus){
             if( data.result == 'success'){
      		      //$(object).parent().hide();
      		      //alert($(id).html());
      		      $(id).html('');
      		      /*DELETE THE FILENAME EXISTS IN THE FILES AREA*/
                  $("#data-company_cv_files").html(function(index,html){
                      return str_delete( filename + ';' , html )               	
                        });
      		      
      		  }//if
             
             }//success 
         });
	 
	 
    	
}

/* THIS FUNCTION IS USED TO DELETE A SUBSTRING WHERE EXISTS IN THE SOUCE STRING */
function str_delete(str_handle,str_source){

    /* FIRSTLY,TRIM THE BACKSPACES */
	str_source = str_source.replace(/\s+/g,"");

    /* IF THE STRING HANDLE EXISTS IN STRING SOURCE ,THEN DELETE IT */
	if( str_source.indexOf(str_handle) >= 0 ){
		var str_pre = str_source.substr(0,str_source.indexOf(str_handle));
        var str_post = str_source.substr( str_source.indexOf(str_handle) + str_handle.length , str_source.length);
        var str_dest = str_pre + str_post;
    }else{
        str_dest = str_source;
    }//else
    return str_dest;
}



function cv_upload_temp(){
    $("#cv_upload_row_content").css('display', 'block');

}

function cv_upload_onsubmit(){
	$(".cv_upload_nonsense_input").css({width:"250px"});
	$(".cv_upload_onsubmit_img").css({display:""});
}
/** function:after uploading the cv file, hide the upload img ,reset the width of inupt label 
 */
function cv_upload_oncomplete(){
	$(".cv_upload_onsubmit_img").css({display:"none"});
	$(".cv_upload_nonsense_input").css({width:""});
	$(".cv_upload_file_ownername").attr("value","Whose CV is this?");
}


/* END OF CV UPLOAD FILE */